package controller

import (
	"haiqiu-go-admin/result"
	"haiqiu-go-admin/service"
	"log"
	"net/http"
)

func (*RoleApi) RoleHandle(w http.ResponseWriter, r *http.Request) {
	switch r.Method {
	case http.MethodGet:
		RoleAPI.roleById(w, r)
		break
	case http.MethodPost:
		RoleAPI.roleAdd(w, r)
		break
	case http.MethodPut:
		RoleAPI.roleUpdate(w, r)
		break
	case http.MethodDelete:
		RoleAPI.roleDeleteBatch(w, r)
		break
	default:
		break
	}
}

// 根据ID查询数据
func (*RoleApi) roleById(w http.ResponseWriter, r *http.Request) {
	page, err := service.RoleById(w, r)
	if err != nil {
		result.Fail(w, r, err)
		return
	}
	result.Ok(w, r, page)
}

// RolePage 分页查询
func (*RoleApi) RolePage(w http.ResponseWriter, r *http.Request) {
	page, err := service.RolePage(w, r)
	if err != nil {
		result.Fail(w, r, err)
		return
	}
	result.Ok(w, r, page)
}

// 新增数据
func (*RoleApi) roleAdd(w http.ResponseWriter, r *http.Request) {
	add, err := service.RoleAdd(w, r)
	if err != nil {
		result.Fail(w, r, err)
		return
	}
	result.Ok(w, r, add)
}

// 批量删除数据
func (*RoleApi) roleDeleteBatch(w http.ResponseWriter, r *http.Request) {
	add, err := service.RoleDeleteBatch(w, r)
	if err != nil {
		result.Fail(w, r, err)
		return
	}
	result.Ok(w, r, add)
}

// 根据ID更新数据
func (*RoleApi) roleUpdate(w http.ResponseWriter, r *http.Request) {
	add, err := service.RoleUpdate(w, r)
	if err != nil {
		result.Fail(w, r, err)
		return
	}
	result.Ok(w, r, add)
}

// RoleList 查询所有数据
func (*RoleApi) RoleList(w http.ResponseWriter, r *http.Request) {
	roleList, err := service.RoleList(w, r)
	if err != nil {
		result.Fail(w, r, err)
		return
	}
	result.Ok(w, r, roleList)
}

// 激活禁用(角色)
func (*RoleApi) Active(w http.ResponseWriter, r *http.Request) {
	active, err := service.ActiveRole(w, r)
	if err != nil {
		log.Println(err.Error())
		result.Fail(w, r, err)
		return
	}
	result.Ok(w, r, active)
}

// 根据角色ID查询角色已授权用户列表
func (*RoleApi) AllocatedList(w http.ResponseWriter, r *http.Request) {
	mapData, err := service.AllocatedList(w, r)
	if err != nil {
		log.Println(err.Error())
		result.Fail(w, r, err)
		return
	}
	result.Ok(w, r, mapData)
}

// 根据角色ID查询角色未授权用户列表
func (*RoleApi) UnallocatedList(w http.ResponseWriter, r *http.Request) {
	mapData, err := service.UnallocatedList(w, r)
	if err != nil {
		log.Println(err.Error())
		result.Fail(w, r, err)
		return
	}
	result.Ok(w, r, mapData)
}

// 批量取消或者批量添加用户授权角色
func (*RoleApi) Authorize(w http.ResponseWriter, r *http.Request) {
	mapData, err := service.Authorize(w, r)
	if err != nil {
		log.Println(err.Error())
		result.Fail(w, r, err)
		return
	}
	result.Ok(w, r, mapData)
}
